/// FileName: world_list_items
/// Author: admin
/// Date: 2021-05-24 16:38
/// Description: 仿到梦空间世界推荐中的推荐项
/// 不使用数据接口，只有UI样式

import 'package:date_format/date_format.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:high_help_apps/common/routes/app_pages.dart';
import 'package:high_help_apps/common/values/colors.dart';

class WorldListItems extends StatelessWidget {
  WorldListItems(
      {this.avatar,
      this.nickname,
      this.uid,
      this.imageSrc,
      this.goods,
      this.time,
      this.tags,
      this.content});

  /// 头像
  final String? avatar;

  /// 昵称
  final String? nickname;

  /// uid
  final int? uid;

  /// 中间图片集合
  final List<String>? imageSrc;

  /// 点赞数量
  final int? goods;

  /// 发布时间
  final int? time;

  /// 标签
  final List<String>? tags;

  /// 内容
  final String? content;

  Container _buildListItemHeader(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(18.5.r),
      height: 77.r,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [_buildHeaderUser(context), _buildHeaderButtons(context)],
      ),
    );
  }

  /// 头部左侧的头像和用户名区域
  GestureDetector _buildHeaderUser(BuildContext context) {
    /// 此处可以被点击，故加上Gesture
    return GestureDetector(
      onTap: () {
        print('头像部分被点击了！');
        Get.toNamed(AppRoutes.USER_FAV_PROFILE,
            parameters: {"uid": uid.toString()});
      },
      child: Row(
        children: [
          ClipOval(
            child: Image.network(avatar ?? ''),
          ),
          SizedBox(
            width: 10.5.r,
          ),
          Text(
            nickname ?? '',
            style: TextStyle(fontWeight: FontWeight.bold),
          )
        ],
      ),
    );
  }

  /// 头部右侧的关注和更多按钮部分
  Row _buildHeaderButtons(BuildContext context) {
    return Row(
      children: [
        _buildFavoriteButton(context),
        SizedBox(
          width: 5.0.r,
        ),
        _buildMoreButton(context),
      ],
    );
  }

  /// 关注按钮
  GestureDetector _buildFavoriteButton(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('点击了关注按钮！');
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 10.0.r),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(5.r)),
            border: Border.all(color: AppColors.primaryElement),
            color: Colors.transparent),
        child: Padding(
          padding: EdgeInsets.all(3.0.r),
          child: Text(
            '关注',
            style: TextStyle(color: AppColors.primaryElement),
          ),
        ),
      ),
    );
  }

  /// 更多按钮
  IconButton _buildMoreButton(BuildContext context) {
    return IconButton(
        onPressed: () {
          print('点击了更多按钮');
        },
        icon: Icon(
          Icons.more_horiz,
          color: AppColors.subTextColor,
        ));
  }

  ///////////////////////////////////////////////////////

  /// 中间的组件，此处的child应该是参数传入过来的组件
  LayoutBuilder _buildCenterWidget() {
    return LayoutBuilder(builder: (context, constraints) {
      final widgetWidth = constraints.maxWidth;
      return Container(
        width: widgetWidth,
        height: widgetWidth,
        child: imageSrc!.length != 1
            ? Swiper(
                scale: 0.9,
                itemBuilder: (BuildContext context, int index) {
                  // 配置图片地址
                  return Image.network(
                    imageSrc![index],
                    fit: BoxFit.cover,
                  );
                },
                // 配置图片数量
                itemCount: imageSrc!.length,
                // 左右箭头
                control: SwiperControl(
                    color: Colors.white60,
                    padding: EdgeInsets.symmetric(horizontal: 15.w)),
              )
            : Image.network(
                imageSrc![0],
                fit: BoxFit.cover,
              ),
      );
    });
  }

  ///////////////////////////////////////////////////////

  Padding _buildBottomToolBar() {
    return Padding(
      padding: EdgeInsets.all(10.0.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [_buildLeftBottomToolBar(), _buildBookMarkIconButton()],
      ),
    );
  }

  Row _buildLeftBottomToolBar() {
    return Row(
      children: [
        _buildFavoriteIconButton(),
        _buildMessageIconButton(),
        _buildShareIconButton(),
      ],
    );
  }

  IconButton _buildFavoriteIconButton() {
    return IconButton(
        onPressed: () {
          print('点击了点赞按钮');
        },
        icon: Icon(
          Icons.favorite,
          color: AppColors.subTextColor,
        ));
  }

  IconButton _buildMessageIconButton() {
    return IconButton(
      onPressed: () {
        print('点击了讨论按钮!');
      },
      icon: Icon(
        Icons.message,
        color: AppColors.subTextColor,
      ),
    );
  }

  IconButton _buildShareIconButton() {
    return IconButton(
      onPressed: () {
        print('点击了分享按钮');
      },
      icon: Icon(
        Icons.share,
        color: AppColors.subTextColor,
      ),
    );
  }

  IconButton _buildBookMarkIconButton() {
    return IconButton(
      onPressed: () {
        print('点击了收藏按钮!');
      },
      icon: Icon(
        Icons.bookmark,
        color: AppColors.subTextColor,
      ),
    );
  }

  ///////////////////////////////////////////////////////

  String _formatDateTime(int time) {
    DateTime currentDate = DateTime.now();
    DateTime paramTime = DateTime.fromMillisecondsSinceEpoch(time);
    Duration deltaTime = currentDate.difference(paramTime);
    switch(deltaTime.inDays) {
      case 0:
        return '今天 ' + formatDate(paramTime, [HH, ':', nn, ':', ss]);
      case 1:
        return '昨天' + formatDate(paramTime, [HH, ':', nn, ':', ss]);
      case 2:
        return '前天 ' + formatDate(paramTime, [HH, ':', nn, ':', ss]);
      default:
        return formatDate(paramTime, [yyyy, '年', mm, '月', d, '日']);
    }
  }

  Padding _buildGreatContainer() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 25.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildSmallSubText('已收到$goods个赞'),
          _buildSmallSubText(
              _formatDateTime(time!)
          ),
        ],
      ),
    );
  }

  Text _buildSmallSubText(String text) {
    return Text(
      text,
      style: TextStyle(fontSize: 12.0.sp, color: AppColors.subTextColor),
    );
  }

  ///////////////////////////////////////////////////////

  Padding _buildBottomIntroduction() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 20.5.r, horizontal: 12.5.r),
      child: _buildRichText(),
    );
  }

  RichText _buildRichText() {
    List<InlineSpan> textSpans = [
      TextSpan(
          text: nickname,
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0.sp)),
      TextSpan(text: '  ')
    ];

    textSpans.addAll(tags!
        .map((e) => TextSpan(
            text: ' #' + e + '# ',
            style: TextStyle(color: Colors.blue),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                print('点击了$e标签');
              }))
        .toList());

    textSpans.add(TextSpan(
        text: '  ' + content!, style: TextStyle(color: Colors.black54)));

    return RichText(
        text: TextSpan(
            style: TextStyle(fontSize: 14.0.sp, color: Colors.black),
            children: textSpans));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          _buildListItemHeader(context),
          _buildCenterWidget(),
          _buildBottomToolBar(),
          _buildGreatContainer(),
          _buildBottomIntroduction()
        ],
      ),
      decoration: BoxDecoration(color: Colors.white),
    );
  }
}
